import os

import pytest

from api.user_api import login
from core.api_util import api_util
from utils.read import base_data


def get_data():
    return base_data.read_data()


def get_ini():
    return base_data.read_ini()


# 登录接口封装  返回'token'
@pytest.fixture(scope='session')
def login_fixture():
    """
    优化headers组装 将返回’token‘变成直接返回’headers‘
    :return:
    """
    data = base_data.read_data()['login_fixture']
    username = data['mobile']
    password = data['password']
    # 登录接口
    result = login(username, password)
    # 实际header：Authorization: JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxNjg4OTIsInVzZXJuYW1lIjoiMTU4MTk3NDk1NTIiLCJleHAiOjE3MjY0ODg5MTUsImVtYWlsIjpudWxsfQ.X7Nzzy0W-xm6016uDAHIbxQmz56bm0NbFSd74GMIKT8
    headers = {
        'Authorization': 'JWT ' + result.body['token']
    }
    return headers, username

@pytest.fixture(scope='session')
def login_fixture_1():
    """
    再进一步优化username的返回 仅返回headers
    :return:
    """
    data = base_data.read_data()['login_fixture']
    username = data['mobile']
    password = data['password']
    # 登录接口
    result = login(username, password)
    # 实际header：Authorization: JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxNjg4OTIsInVzZXJuYW1lIjoiMTU4MTk3NDk1NTIiLCJleHAiOjE3MjY0ODg5MTUsImVtYWlsIjpudWxsfQ.X7Nzzy0W-xm6016uDAHIbxQmz56bm0NbFSd74GMIKT8
    headers = {
        'Authorization': 'JWT ' + result.body['token']
    }
    return headers

@pytest.fixture
def login_fixture_0():
    """
    优化前
    :return:
    """
    if 'token' not in os.environ:
        data = base_data.read_data()['login_fixture']
        username = data['mobile']
        password = data['password']
        # 登录接口
        result = login(username, password)
        os.environ['token'] = result.body['token']
        os.environ['mobile'] = str(username)
        return result.body['token'], username
    else:
        return os.environ['token'], os.environ['mobile']


def test(login_fixture):
    print(login_fixture)
